﻿using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces.SystemRole;
using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Net.Mail;
using gAMS_Sacombank.Web.Services.AssetManagement;
using gAMS_Sacombank.Web.Services.AssetManagement.Functions;
using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces.AssCommon;
using gAMS_Sacombank.Web.Services.AssetManagement.Interfaces.AssetManager;

namespace gAMS_Sacombank.Web.Services.AssetManagement.Implement
{
    public class ImplementInterface : ITLUSER, ITLSYSROLE, ITLMENU, IRegions, IBranch, ISupplierType, ISupplier, IGoodsType, IGoods, IASSET_TYPE, IASSET_GROUP, IDEPARTMENT, IDIVISION, IARGUMENT, IUnit
    {               

        #region [Public Functions]

        public int GetDayOfYear(int year)
        {
            return (new DateTime(year + 1, 1, 1) - new DateTime(year, 1, 1)).Days;
        }

        public string SetAutoId(string id, int maxlength)
        {
            while (id.Length < maxlength)
            {
                id = "0" + id;
            }
            return id;
        }        

        #endregion

        #region[TL_USER]

        public TL_USER CheckUserLogin(string userName, string password)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from user in dataContext.TL_USERs where user.TLNANME == userName && user.Password == password select user).Single();
                    return result;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public LoginData CheckLogin(string userName, string password, int indexConnect)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var currentUser = (from user in dataContext.TL_USERs where user.TLNANME == userName && user.Password == password select user).SingleOrDefault();
                    if (currentUser == null) return null;

                    //User
                    LoginData loginData = new LoginData();
                    loginData.User = currentUser;                    

                    //Role                    
                    var currentRole = (from item in dataContext.TL_SYSROLEDETAILs where item.ROLE_ID.Equals(currentUser.RoleName) select item).ToList();
                    loginData.Roles = currentRole;

                    //connection = lstConnect[indexConnect];

                    return loginData;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        public IEnumerable<TL_USER> GetByAllTLUSER()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.TL_USERs.ToList();
                    return result;
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
        public bool ContainsTLUSER(TL_USER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var obj = (
                        from item in dataContext.TL_USERs
                        where item.TLNANME == data.TLNANME
                        select item
                        );

                    return (obj != null && obj.Count() > 0);
                }
            }
            catch (Exception ex)
            {                
                return false;
            }
        }
        public string InsertTLUSER(TL_USER data)
        {
            try
            {
                if (ContainsTLUSER(data)) return ListMessage.Existed;
                using (var dataContext = new AssetDataContext())
                {                    
                    dataContext.TL_USERs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public bool UpdateTLUSER(TL_USER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from current in dataContext.TL_USERs where current.TLID == data.TLID select current).Single();
                    
                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool DeleteTLUSER(TL_USER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from current in dataContext.TL_USERs where current.TLNANME.Equals(data.TLNANME) select current).Single();
                    dataContext.TL_USERs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public IEnumerable<TL_USER> GetByTopTLUSER(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [TL_USER]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [TL_USER] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<TL_USER> results = data.ExecuteQuery<TL_USER>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string InsertAllTLUSER(ObservableCollection<TL_USER> lstUser)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {                    
                    dataContext.TL_USERs.InsertAllOnSubmit(lstUser);
                    dataContext.SubmitChanges();
                    
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {                
                return ex.Message;
            }
        }

        public string ChangePassTLUSER(string userName, string code, string pass)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var currentUser = (from item in dataContext.TL_USERs where item.TLNANME.Equals(userName) select item).SingleOrDefault();
                    if (currentUser == null) return "NotExisted";
                    if (currentUser.SECUR_CODE != code) return "CodeFail";

                    currentUser.Password = pass;
                    currentUser.SECUR_CODE = "";
                    
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string SendCodeTLUSER(string userName)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    var currentUser = (from item in dataContext.TL_USERs where item.TLNANME.Equals(userName) select item).SingleOrDefault();
                    if (currentUser == null) return "NotExisted";

                    currentUser.SECUR_CODE = System.Guid.NewGuid().ToString("N").Substring(0, 10);
                    string body = "Xin chào " + currentUser.TLFullName + ",\n\n\t"
                        + " Mã dùng để đổi mật khẩu (Code): " + currentUser.SECUR_CODE + "\n\t"
                        + " p/s: Code chỉ dùng để thay đổi mật khẩu 1 lần duy nhất.\n\n"
                        + " Trân Trọng!\n"
                        + "-----------------\n"
                        + " Công Ty TNHH Hoàn Cầu - GSOFT";

                    Mail(currentUser.EMAIL, body, "[gEduBanking] - Password Code");
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public void Mail(string EmailTo, string MailBody, string MailSubject)
        {
            string eMail = "gedubanking@gmail.com";
            string Pass = "gsoftedu";
            MailMessage mail = new MailMessage();
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            mail.From = new MailAddress("gEduBanking<" + eMail + ">");
            mail.To.Add(EmailTo); //nhap dia chi mail gui den
            mail.Subject = MailSubject;
            mail.Body = MailBody;
            SmtpServer.Port = 587;
            SmtpServer.Credentials = new System.Net.NetworkCredential(eMail, Pass);
            SmtpServer.EnableSsl = true;
            SmtpServer.Send(mail);                
        }

        #endregion                

        #region[TL_SYSROLE]

        public IEnumerable<TL_SYSROLE> GetByAllTLSYSROLE()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.TL_SYSROLEs.ToList();
                    return result;
                }
            }
            catch (Exception ex)
            {                
                return null;
            }
        }
        public string InsertTLSYSROLE(TL_SYSROLE data, List<TL_SYSROLEDETAIL> lstRoleDetail)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    //Kiem tra ton tai
                    var item = (from it in dataContext.TL_SYSROLEs where it.ROLE_ID.Equals(data.ROLE_ID) select it).SingleOrDefault();
                    if (item != null) return ListMessage.Existed;

                    //Insert SYSROLE
                    dataContext.TL_SYSROLEs.InsertOnSubmit(data);
                    
                    //Insert SYSROLEDETAIL
                    dataContext.TL_SYSROLEDETAILs.InsertAllOnSubmit(lstRoleDetail);

                    dataContext.SubmitChanges();

                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        public Boolean UpdateTLSYSROLE(TL_SYSROLE data, List<TL_SYSROLEDETAIL> lstUpdate, List<TL_SYSROLEDETAIL> lstInsert, List<string> lstDelete)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    //Update TL_SYSROLE
                    var resultRole = (from item1 in dataContext.TL_SYSROLEs where item1.ROLE_ID == data.ROLE_ID select item1).Single();
                    MyHelper.Copy<TL_SYSROLE>(resultRole, data);

                    //Update TL_SYSROLEDETAIL
                    //Update
                    foreach (var roleDetail in lstUpdate)
                    {
                        var resuleItemUpdate = (from item in dataContext.TL_SYSROLEDETAILs
                                                where item.ROLE_ID.Equals(data.ROLE_ID) && item.MENU_ID.Equals(roleDetail.MENU_ID)
                                                select item).Single();
                        MyHelper.Copy<TL_SYSROLEDETAIL>(resuleItemUpdate, roleDetail);
                    }
                    //Insert
                    dataContext.TL_SYSROLEDETAILs.InsertAllOnSubmit(lstInsert);

                    //Delete
                    var lstDeleteDetail = (from item in dataContext.TL_SYSROLEDETAILs where lstDelete.Contains(item.MENU_ID) && item.ROLE_ID.Equals(data.ROLE_ID) select item);
                    dataContext.TL_SYSROLEDETAILs.DeleteAllOnSubmit(lstDeleteDetail);

                    dataContext.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public Boolean DeleteTLSYSROLE(TL_SYSROLE data)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    var item = (from item1 in dataContext.TL_SYSROLEs where item1.ROLE_ID.Equals(data.ROLE_ID) select item1).Single();
                    dataContext.TL_SYSROLEs.DeleteOnSubmit(item);
                    var lstItemRoleDetail = (from it in dataContext.TL_SYSROLEDETAILs where it.ROLE_ID.Equals(data.ROLE_ID) select it);
                    dataContext.TL_SYSROLEDETAILs.DeleteAllOnSubmit(lstItemRoleDetail);

                    dataContext.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                return false;
            }
        }
        public IEnumerable<TL_SYSROLE> GetByTopTLSYSROLE(string _top, string _where, string _order)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    string query = "Select * from [TL_SYSROLE]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [TL_SYSROLE] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<TL_SYSROLE> results = dataContext.ExecuteQuery<TL_SYSROLE>(@query);

                    return results.ToList();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public IEnumerable<TL_SYSROLEDETAIL> GetByTopTLROLEDETAIL(string _top, string _where, string _order)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    string query = "Select * from [TL_SYSROLEDETAIL]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [TL_SYSROLEDETAIL] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<TL_SYSROLEDETAIL> results = dataContext.ExecuteQuery<TL_SYSROLEDETAIL>(@query);

                    return results.ToList();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region[TL_MENU]

        public IEnumerable<TL_MENU> GetByAllTLMENU()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.TL_MENUs.ToList();
                    return result;
                }
            }
            catch (Exception ex)
            {                
                return null;
            }
        }

        public string InsertTLMENU(TL_MENU data)
        {
            try
            {               
                using (var dataContext = new AssetDataContext())
                {
                    dataContext.TL_MENUs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();

                    string query = "Select Top 1 MENU_ID From [TL_MENU] Order by MENU_ID DESC";
                    IEnumerable<int> id = dataContext.ExecuteQuery<int>(@query);
                    query = id.First().ToString();
                    return query;
                }
            }
            catch (Exception ex)
            {                
                return "";
            }
        }

        public bool UpdateTLMENU(TL_MENU data)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    var item = (from item1 in dataContext.TL_MENUs where item1.MENU_ID.Equals(data.MENU_ID) select item1).Single();
                    MyHelper.Copy<TL_MENU>(item, data);
                    dataContext.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {                
                return false;
            }
        }

        public bool DeleteTLMENU(TL_MENU data)
        {
            try
            {                
                using (var dataContext = new AssetDataContext())
                {
                    var item = (from item1 in dataContext.TL_MENUs where item1.MENU_ID.Equals(data.MENU_ID) select item1).Single();
                    dataContext.TL_MENUs.DeleteOnSubmit(item);

                    dataContext.SubmitChanges();

                    string query = "Update TL_MENU Set MENU_PARENT = '' where MENU_PARENT = '" + item.MENU_ID + "' "
                        + " DELETE TL_SYSROLEDETAIL Where MENU_ID='" + item.MENU_ID + "' ";
                    dataContext.ExecuteQuery<int>(@query);
                    return true;
                }
            }
            catch (Exception ex)
            {                
                return false;
            }
        }

        public IEnumerable<TL_MENU> GetByTopTLMENU(string _top, string _where, string _order)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    string query = "Select * from [TL_MENU]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [TL_MENU] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<TL_MENU> results = dataContext.ExecuteQuery<TL_MENU>(@query);

                    return results.ToList();
                }
            }
            catch (Exception ex)
            {               
                return null;
            }
        }

        public IEnumerable<TL_MENU> GetParentTLMENU()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    string query = "Select * from [TL_MENU] where MENU_PARENT = '' ";                    

                    IEnumerable<TL_MENU> results = dataContext.ExecuteQuery<TL_MENU>(@query);

                    return results.ToList();
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        #endregion        

        #region [REGION]

        public IEnumerable<CM_REGION> GetByAllRegion()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_REGIONs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertRegion(CM_REGION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_REGIONs where item.REGION_CODE.Equals(data.REGION_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_REGIONs
                                  where item.REGION_ID.Equals(data.REGION_ID)
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.REGION_ID;
                        data.REGION_ID = (int.Parse(id.Replace("R", "")) + 1).ToString("R0000");
                    }
                    else
                        data.REGION_ID = "R0001";

                    dataContext.CM_REGIONs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.REGION_ID;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateRegion(CM_REGION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_REGIONs
                                  where item.REGION_ID.Equals(data.REGION_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteRegion(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_REGIONs
                                  where item.REGION_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_REGIONs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_REGION> GetByTopRegion(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_REGIONS]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_REGIONS] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_REGION> results = data.ExecuteQuery<CM_REGION>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveRegion(CM_REGION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_REGIONs where current.REGION_ID == data.REGION_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion

        #region [BRANCH]

        public IEnumerable<CM_BRANCH> GetByAllBranch()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_BRANCHes.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertBranch(CM_BRANCH data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_BRANCHes where item.BRANCH_CODE.Equals(data.BRANCH_CODE) select item).FirstOrDefault();
                    if(existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_BRANCHes
                                  orderby item.BRANCH_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.BRANCH_ID;
                        data.BRANCH_ID = (int.Parse(id.Replace("DV", "")) + 1).ToString("DV0000");
                    }
                    else
                        data.BRANCH_ID = "DV0001";

                    dataContext.CM_BRANCHes.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateBranch(CM_BRANCH data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_BRANCHes
                                  where item.BRANCH_ID.Equals(data.BRANCH_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteBranch(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_BRANCHes
                                  where item.BRANCH_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_BRANCHes.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_BRANCH> GetByTopBranch(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_BRANCH]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_BRANCH] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_BRANCH> results = data.ExecuteQuery<CM_BRANCH>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveBranch(CM_BRANCH data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_BRANCHes where current.BRANCH_ID == data.BRANCH_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [SUPPLIERTYPE]

        public IEnumerable<CM_SUPPLIERTYPE> GetByAllSupplierType()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_SUPPLIERTYPEs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertSupplierType(CM_SUPPLIERTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_SUPPLIERTYPEs where item.SUP_TYPE_CODE.Equals(data.SUP_TYPE_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_SUPPLIERTYPEs
                                  orderby item.SUP_TYPE_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.SUP_TYPE_ID;
                        data.SUP_TYPE_ID = (int.Parse(id.Replace("LNCC", "")) + 1).ToString("LNCC0000");
                    }
                    else
                        data.SUP_TYPE_ID = "LNCC0001";

                    dataContext.CM_SUPPLIERTYPEs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateSupplierType(CM_SUPPLIERTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_SUPPLIERTYPEs
                                  where item.SUP_TYPE_ID.Equals(data.SUP_TYPE_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteSupplierType(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_SUPPLIERTYPEs
                                  where item.SUP_TYPE_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_SUPPLIERTYPEs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_SUPPLIERTYPE> GetByTopSupplierType(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_SUPPLIERTYPE]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_SUPPLIERTYPE] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_SUPPLIERTYPE> results = data.ExecuteQuery<CM_SUPPLIERTYPE>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveSupplierType(CM_SUPPLIERTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_SUPPLIERTYPEs where current.SUP_TYPE_ID == data.SUP_TYPE_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [SUPPLIER]

        public IEnumerable<CM_SUPPLIER> GetByAllSupplier()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_SUPPLIERs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertSupplier(CM_SUPPLIER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_SUPPLIERs where item.SUP_CODE.Equals(data.SUP_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_SUPPLIERs
                                  orderby item.SUP_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.SUP_ID;
                        data.SUP_ID = (int.Parse(id.Replace("NCC", "")) + 1).ToString("NCC0000");
                    }
                    else
                        data.SUP_ID = "NCC0001";

                    dataContext.CM_SUPPLIERs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateSupplier(CM_SUPPLIER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_SUPPLIERs
                                  where item.SUP_ID.Equals(data.SUP_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteSupplier(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_SUPPLIERs
                                  where item.SUP_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_SUPPLIERs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_SUPPLIER> GetByTopSupplier(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_SUPPLIER]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_SUPPLIER] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_SUPPLIER> results = data.ExecuteQuery<CM_SUPPLIER>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveSupplier(CM_SUPPLIER data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_SUPPLIERs where current.SUP_ID == data.SUP_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [UNIT]

        public IEnumerable<CM_UNIT> GetByAllUnit()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_UNITs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_UNITs where item.UNIT_CODE.Equals(data.UNIT_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_UNITs
                                  orderby item.UNIT_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.UNIT_ID;
                        data.UNIT_ID = (int.Parse(id.Replace("DVT", "")) + 1).ToString("DVT0000");
                    }
                    else
                        data.UNIT_ID = "DVT0001";

                    dataContext.CM_UNITs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_UNITs
                                  where item.UNIT_ID.Equals(data.UNIT_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteUnit(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_UNITs
                                  where item.UNIT_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_UNITs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_UNIT> GetByTopUnit(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_UNIT]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_UNIT] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_UNIT> results = data.ExecuteQuery<CM_UNIT>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveUnit(CM_UNIT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_UNITs where current.UNIT_ID == data.UNIT_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [GOODSTYPE]

        public IEnumerable<CM_GOODSTYPE> GetByAllGoodsType()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_GOODSTYPEs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertGoodsType(CM_GOODSTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_GOODSTYPEs where item.GD_TYPE_CODE.Equals(data.GD_TYPE_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_GOODSTYPEs
                                  orderby item.GD_TYPE_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.GD_TYPE_ID;
                        data.GD_TYPE_ID = (int.Parse(id.Replace("LHH", "")) + 1).ToString("LHH0000");
                    }
                    else
                        data.GD_TYPE_ID = "LHH0001";

                    dataContext.CM_GOODSTYPEs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateGoodsType(CM_GOODSTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_GOODSTYPEs
                                  where item.GD_TYPE_ID.Equals(data.GD_TYPE_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteGoodsType(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_GOODSTYPEs
                                  where item.GD_TYPE_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_GOODSTYPEs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_GOODSTYPE> GetByTopGoodsType(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_GOODSTYPE]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_GOODSTYPE] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_GOODSTYPE> results = data.ExecuteQuery<CM_GOODSTYPE>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveGoodsType(CM_GOODSTYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_GOODSTYPEs where current.GD_TYPE_ID == data.GD_TYPE_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [GOODS]
        public IEnumerable<CM_GOOD> GetByAllGoods()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_GOODs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertGoods(CM_GOOD data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_GOODs where item.GD_CODE.Equals(data.GD_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_GOODs
                                  orderby item.GD_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.GD_ID;
                        data.GD_ID = (int.Parse(id.Replace("HH", "")) + 1).ToString("HH0000");
                    }
                    else
                        data.GD_ID = "HH0001";

                    dataContext.CM_GOODs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string UpdateGoods(CM_GOOD data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_GOODs
                                  where item.GD_ID.Equals(data.GD_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteGoods(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_GOODs
                                  where item.GD_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_GOODs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_GOOD> GetByTopGoods(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_GOODS]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_GOODS] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_GOOD> results = data.ExecuteQuery<CM_GOOD>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveGoods(CM_GOOD data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_GOODs where current.GD_ID == data.GD_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [ASS_TYPE]

        public IEnumerable<ASS_TYPE> GetByAllAsset_Type()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.ASS_TYPEs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertAsset_Type(ASS_TYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.ASS_TYPEs where item.TYPE_CODE.Equals(data.TYPE_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.ASS_TYPEs
                                  orderby item.TYPE_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.TYPE_ID;
                        data.TYPE_ID = (int.Parse(id.Replace("AT", "")) + 1).ToString("AT0000");
                    }
                    else
                        data.TYPE_ID = "AT0001";

                    dataContext.ASS_TYPEs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.TYPE_ID;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string UpdateAsset_Type(ASS_TYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.ASS_TYPEs
                                  where item.TYPE_ID.Equals(data.TYPE_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteAsset_Type(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.ASS_TYPEs
                                  where item.TYPE_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.ASS_TYPEs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<ASS_TYPE> GetByTopAsset_Type(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [ASS_TYPE]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [ASS_TYPE] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<ASS_TYPE> results = data.ExecuteQuery<ASS_TYPE>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveAsset_Type(ASS_TYPE data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.ASS_TYPEs where current.TYPE_ID == data.TYPE_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [ASS_GROUP]

        public IEnumerable<ASS_GROUP> GetByAllAsset_GROUP()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.ASS_GROUPs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertAsset_GROUP(ASS_GROUP data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.ASS_GROUPs where item.GROUP_CODE.Equals(data.GROUP_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.ASS_GROUPs
                                  orderby item.GROUP_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.GROUP_ID;
                        data.GROUP_ID = (int.Parse(id.Replace("AG", "")) + 1).ToString("AG0000");
                    }
                    else
                        data.GROUP_ID = "AG0001";

                    dataContext.ASS_GROUPs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.GROUP_ID;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string UpdateAsset_GROUP(ASS_GROUP data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.ASS_GROUPs
                                  where item.GROUP_ID.Equals(data.GROUP_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteAsset_GROUP(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.ASS_GROUPs
                                  where item.GROUP_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.ASS_GROUPs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<ASS_GROUP> GetByTopAsset_GROUP(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [ASS_GROUP]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [ASS_GROUP] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<ASS_GROUP> results = data.ExecuteQuery<ASS_GROUP>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveAsset_GROUP(ASS_GROUP data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNGROUP
                    var result = (from current in dataContext.ASS_GROUPs where current.GROUP_ID == data.GROUP_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [DEPARTMENT]

        public IEnumerable<CM_DEPARTMENT> GetByAllDEPARTMENT()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_DEPARTMENTs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertDEPARTMENT(CM_DEPARTMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_DEPARTMENTs where item.DEP_CODE.Equals(data.DEP_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_DEPARTMENTs
                                  orderby item.DEP_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.DEP_ID;
                        data.DEP_ID = (int.Parse(id.Replace("DP", "")) + 1).ToString("DP0000");
                    }
                    else
                        data.DEP_ID = "DP0001";

                    dataContext.CM_DEPARTMENTs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.DEP_ID;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string UpdateDEPARTMENT(CM_DEPARTMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_DEPARTMENTs
                                  where item.DEP_ID.Equals(data.DEP_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteDEPARTMENT(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_DEPARTMENTs
                                  where item.DEP_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_DEPARTMENTs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_DEPARTMENT> GetByTopDEPARTMENT(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_DEPARTMENT]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_DEPARTMENT] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_DEPARTMENT> results = data.ExecuteQuery<CM_DEPARTMENT>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveDEPARTMENT(CM_DEPARTMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_DEPARTMENTs where current.DEP_ID == data.DEP_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [DIVISION]

        public IEnumerable<CM_DIVISION> GetByAllDIVISION()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_DIVISIONs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertDIVISION(CM_DIVISION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_DIVISIONs where item.DIV_CODE.Equals(data.DIV_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_DIVISIONs
                                  orderby item.DIV_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.DIV_ID;
                        data.DIV_ID = (int.Parse(id.Replace("DIV", "")) + 1).ToString("DIV0000");
                    }
                    else
                        data.DIV_ID = "DIV0001";

                    dataContext.CM_DIVISIONs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.DIV_ID;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string UpdateDIVISION(CM_DIVISION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_DIVISIONs
                                  where item.DIV_ID.Equals(data.DIV_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteDIVISION(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_DIVISIONs
                                  where item.DIV_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_DIVISIONs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_DIVISION> GetByTopDIVISION(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_DIVISION]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_DIVISION] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_DIVISION> results = data.ExecuteQuery<CM_DIVISION>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveDIVISION(CM_DIVISION data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_DIVISIONs where current.DIV_ID == data.DIV_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
        #region [ARGUMENT]

        public IEnumerable<CM_ARGUMENT> GetByAllArgument()
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = dataContext.CM_ARGUMENTs.ToList();
                    return result;
                }

            }
            catch (Exception)
            {
                return null;
            }
        }

        public string InsertArgument(CM_ARGUMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    //Check existed
                    var existItem = (from item in dataContext.CM_ARGUMENTs where item.AGR_CODE.Equals(data.AGR_CODE) select item).FirstOrDefault();
                    if (existItem != null) return ListMessage.Existed;
                    var result = (from item in dataContext.CM_ARGUMENTs
                                  orderby item.ARG_ID descending
                                  select item).FirstOrDefault();
                    if (result != null)
                    {
                        string id = result.ARG_ID;
                        data.ARG_ID = (int.Parse(id.Replace("ARG", "")) + 1).ToString("ARG0000");
                    }
                    else
                        data.ARG_ID = "ARG0001";

                    dataContext.CM_ARGUMENTs.InsertOnSubmit(data);
                    dataContext.SubmitChanges();
                    return data.ARG_ID;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public string UpdateArgument(CM_ARGUMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_ARGUMENTs
                                  where item.ARG_ID.Equals(data.ARG_ID)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    MyHelper.Copy(result, data);

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string DeleteArgument(string id)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {
                    var result = (from item in dataContext.CM_ARGUMENTs
                                  where item.ARG_ID.Equals(id)
                                  select item).SingleOrDefault();
                    if (result == null) return ListMessage.NotExisted;

                    dataContext.CM_ARGUMENTs.DeleteOnSubmit(result);
                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<CM_ARGUMENT> GetByTopArgument(string _top, string _where, string _order)
        {
            try
            {
                using (var data = new AssetDataContext())
                {
                    string query = "Select * from [CM_ARGUMENT]";
                    if (!_top.Equals(""))
                        query = " Select top " + _top + " * from [CM_ARGUMENT] ";
                    if (!_where.Equals(""))
                        query += " where " + _where + " ";
                    if (!_order.Equals(""))
                        query += " order by " + _order;

                    IEnumerable<CM_ARGUMENT> results = data.ExecuteQuery<CM_ARGUMENT>(@query);

                    return results.ToList();
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        public string ApproveArgument(CM_ARGUMENT data)
        {
            try
            {
                using (var dataContext = new AssetDataContext())
                {

                    //update LNTYPE
                    var result = (from current in dataContext.CM_ARGUMENTs where current.ARG_ID == data.ARG_ID select current).Single();

                    result.AUTH_STATUS = "A";
                    result.CHECKER_ID = data.CHECKER_ID;
                    result.APPROVE_DT = data.APPROVE_DT;

                    dataContext.SubmitChanges();
                    return ListMessage.True;
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        #endregion
    }
}